package com.itheima.controller;

import com.itheima.VO.OrderDataVO;
import com.itheima.common.entity.Result;
import com.itheima.pojo.OrderSettingVO;
import com.itheima.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.List;

@RequestMapping("/ordersetting")
@RestController
@Slf4j
public class OrderSettingController {

    @Autowired
    private OrderSettingService orderSettingService;
    @Autowired
    private HttpServletResponse response;


    @GetMapping("/download")
    public void exportData(@RequestParam("filename") String filename) {

        orderSettingService.exportordersetting(filename,response);

    }

    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(@RequestParam("month") String month){
        log.info("根据月份查询订单");
//        DateTimeFormatter format= DateTimeFormatter.ofPattern("yyyy-MM");
        DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendPattern("yyyy-MM").parseDefaulting(ChronoField.DAY_OF_MONTH, 1).toFormatter();
        if (month.length()==6){

            StringBuilder sb = new StringBuilder(month);
            sb.insert(5, '0');
            month=sb.toString();
        }

        List<OrderSettingVO> list=orderSettingService.getOrderSettingByMonth(month);
        return Result.success(list);
    }

    @PostMapping("/editNumberByOrderDate")
    public Result editNumberByOrderDate(@RequestBody OrderDataVO date){
        log.info("根据日期修改可预约人数");
        orderSettingService.editnumberByOrderDate(date);
        return Result.success();
    }



}
